package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.pojo.TbContentExample;
import com.usian.redis.RedisClient;
import com.usian.service.ContentService;
import com.usian.utils.AdNode;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ContentServiceImpl implements ContentService {

    @Value("${AD_CATEGORY_ID}")
    private Long AD_CATEGORY_ID;

    @Value("${AD_HEIGHT}")
    private Integer AD_HEIGHT;

    @Value("${AD_WIDTH}")
    private Integer AD_WIDTH;

    @Value("${AD_HEIGHTB}")
    private Integer AD_HEIGHTB;

    @Value("${AD_WIDTHB}")
    private Integer AD_WIDTHB;

    @Value("${PORTAL_AD_KEY}")
    private String PORTAL_AD_KEY;

    @Autowired
    RedisClient redisClient;

    @Autowired
    TbContentCategoryMapper tbContentCategoryMapper;

    @Autowired
    TbContentMapper tbContentMapper;

    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Integer id) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(Long.valueOf(id));
        List<TbContentCategory> categoryList = tbContentCategoryMapper.selectByExample(example);
        return categoryList;
    }

    /**
     * 内容分类管理添加接口
     *
     * @param tbContentCategory
     * @return
     */
    @Override
    public Integer insertContentCategory(TbContentCategory tbContentCategory) {

        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setIsParent(false);
        tbContentCategory.setStatus(1);
        Integer integer = tbContentCategoryMapper.insertSelective(tbContentCategory);
        TbContentCategory category = tbContentCategoryMapper.selectByPrimaryKey(tbContentCategory.getParentId());
        if (category != null && !category.getIsParent()) {
            category.setIsParent(true);
            category.setUpdated(new Date());
            tbContentCategoryMapper.updateByPrimaryKeySelective(category);
        }
        return integer;
    }

    /**
     * 内容分类删除接口
     *
     * @param categoryId
     * @return
     */
    @Override
    public Integer deleteContentCategoryById(Long categoryId) {
        //查询当前节点
        TbContentCategory tbContentCategory =
                this.tbContentCategoryMapper.selectByPrimaryKey(categoryId);
        //父节点 不允许删除
        if (tbContentCategory.getIsParent()) {
            return 0;
        }
        //不是父节点
        Integer num = tbContentCategoryMapper.deleteByPrimaryKey(categoryId);

        //当前节点的兄弟节点
        TbContentCategoryExample tbContentCategoryExample = new
                TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria =
                tbContentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(tbContentCategory.getParentId());
        List<TbContentCategory> tbContentCategoryList =
                tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        //删除之后如果父节点没有孩子，则修改isParent为false

        if (tbContentCategoryList.size() == 0) {
            TbContentCategory parenttbContentCategory = new TbContentCategory();
            parenttbContentCategory.setId(tbContentCategory.getParentId());
            parenttbContentCategory.setIsParent(false);
            parenttbContentCategory.setUpdated(new Date());
            tbContentCategoryMapper.updateByPrimaryKeySelective(
                    parenttbContentCategory);
        }
        return num;
    }

    /**
     * 内容分类管理修改接口
     *
     * @param tbContentCategory
     * @return
     */
    @Override
    public Integer updateContentCategory(TbContentCategory tbContentCategory) {
        tbContentCategory.setUpdated(new Date());
        Integer integer = tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
        return integer;
    }


    /**
     * 内容管理查询接口
     *
     * @param categoryId
     * @param pages
     * @param rows
     * @return
     */
    @Override
    public PageResult selectTbContentAllByCategoryId(Long categoryId, Integer pages, Integer rows) {
        PageHelper.startPage(pages, rows);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> tbContents = tbContentMapper.selectByExample(example);
        PageInfo<TbContent> info = new PageInfo<>(tbContents);
        PageResult result = new PageResult();
        result.setResult(info.getList());
        result.setPageIndex(info.getPages());
        result.setTotalPage(info.getPageNum());
        return result;
    }


    /**
     * 内容管理  添加接口
     *
     * @param tbContent
     * @return
     */
    @Override
    public Integer insertTbContent(TbContent tbContent) {
        tbContent.setCreated(new Date());
        tbContent.setUpdated(new Date());
        Integer num = tbContentMapper.insertSelective(tbContent);
        redisClient.del(PORTAL_AD_KEY);
        return num;
    }

    @Override
    public Integer deleteContentByIds(Long ids) {
        Integer num = tbContentMapper.deleteByPrimaryKey(ids);
        redisClient.del(PORTAL_AD_KEY);
        return num;
    }

    @Override
    public List<AdNode> selectFrontendContentByAD() {
        List<AdNode> adNodeList =
                (List<AdNode>) redisClient.hget(PORTAL_AD_KEY, AD_CATEGORY_ID + "");
        if (adNodeList != null) {
            return adNodeList;
        }
        // 查询TbContent
        TbContentExample tbContentExample = new TbContentExample();
        TbContentExample.Criteria criteria = tbContentExample.createCriteria();
        criteria.andCategoryIdEqualTo(AD_CATEGORY_ID);
        List<TbContent> tbContentList =
                tbContentMapper.selectByExample(tbContentExample);
        adNodeList = new ArrayList<>();
        for (TbContent tbContent : tbContentList) {
            AdNode adNode = new AdNode();
            adNode.setSrc(tbContent.getPic());
            adNode.setSrcB(tbContent.getPic2());
            adNode.setHref(tbContent.getUrl());
            adNode.setHeight(AD_HEIGHT);
            adNode.setWidth(AD_WIDTH);
            adNode.setHeightB(AD_HEIGHTB);
            adNode.setWidthB(AD_WIDTHB);
            adNodeList.add(adNode);
        }
        redisClient.hset(PORTAL_AD_KEY, AD_CATEGORY_ID + "", adNodeList);
        return adNodeList;
    }
}
